package com.buyi.easyhome.util

import android.content.Context
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.text.format.Formatter
import java.net.Inet4Address
import java.net.InetAddress
import java.net.NetworkInterface

object NetworkEnvironmentUtil {
    /**
     * 反射拿热点是否开启
     */
    fun isAPEnabled(context: Context): Boolean {
        try {
            val wifiManager = context.getApplicationContext()
                .getSystemService(Context.WIFI_SERVICE) as WifiManager
            val method = wifiManager.javaClass.getDeclaredMethod("getWifiApState")
            val field = wifiManager.javaClass.getDeclaredField("WIFI_AP_STATE_ENABLED")
            val value_wifi_enabled = field.get(wifiManager) as Int
            return (method.invoke(wifiManager) as Int) == value_wifi_enabled
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    /**
     * 获取WiFi网络的IPv4地址
     *
     * @return 可能为null
     */
    fun getWifiIp(context: Context): String? {
        try {
            return Formatter.formatIpAddress(
                (context.getApplicationContext()
                    .getSystemService(Context.WIFI_SERVICE) as WifiManager)
                    .getConnectionInfo().getIpAddress()
            )
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    /**
     * 获取本地网络环境所有可用的IPv4地址
     *
     * @return 类似"192.168.1.101"的IP地址集
     */
    fun getLocalIpv4Addresses(): ArrayList<String> {
        val result = java.util.ArrayList<String>()
        try {
            val networkInterfaces =
                NetworkInterface.getNetworkInterfaces()
            var networkInterface: NetworkInterface?
            while ((networkInterfaces.nextElement().also { networkInterface = it }) != null) {
                val inetAddresses =
                    networkInterface!!.getInetAddresses()
                try {
                    var inetAddress: InetAddress?
                    while ((inetAddresses.nextElement().also { inetAddress = it }) != null) {
                        if (!inetAddress!!.isLoopbackAddress() && (inetAddress is Inet4Address)) {
                            val ip = inetAddress.getHostAddress()
                            if (!result.contains(ip)) result.add(ip)
                        }
                    }
                } catch (e: Exception) {
                    //
                }
            }
        } catch (e: Exception) {
            //
        }
        return result
    }

    fun isWifiConnected(context: Context): Boolean {
        return isNetworkConnected(context, ConnectivityManager.TYPE_WIFI)
    }

    fun isCellularNetworkConnected(context: Context): Boolean {
        return isNetworkConnected(context, ConnectivityManager.TYPE_MOBILE)
    }

    private fun isNetworkConnected(context: Context, type: Int): Boolean {
        try {
            val connectivityManager =
                context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = connectivityManager.getActiveNetworkInfo()
            return networkInfo != null && networkInfo.getType() == type
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }
}
